package com.witframework.plugin.lucene.searcher.impl;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.FSDirectory;

import com.witframework.plugin.lucene.SearchException;
import com.witframework.util.ArrayExpander;

public class SimpleSearcher extends AbstractSearcher {

	public Hits search(String queryString) throws SearchException {
		Analyzer analyzer = new ChineseAnalyzer();
		for(String dir:indexLocations){
			File indexDir = new File(dir);
	        FSDirectory directory;
			try {
				directory = FSDirectory.getDirectory(indexDir);
			} catch (IOException e) {
				throw new SearchException("find index location error.",e);
			}
			IndexSearcher indexSearcher;
			try {
				indexSearcher = new IndexSearcher(directory);
				//在“abstracts”摘要上搜索
				QueryParser parser = new QueryParser("contents",analyzer);
				Query query = parser.parse(queryString);
			    Hits results = indexSearcher.search(query);
			    System.out.println(results.length() + " search results for query " + queryString);
			    return results;
			} catch (Exception e) {
				throw new SearchException("search error.",e);
			}

		}
		return null;
	}

	public Hits search(String queryString, String[] resource)
			throws SearchException {
		Analyzer analyzer = new ChineseAnalyzer();

		//组装搜索字符串数组
		String[] queries=new String[]{null,queryString};
		for(int i=0;i<resource.length;i++){
			if(queries[0]!=null)
				queries[0]=queries[0]+" or ";
			queries[0]+=resource[i];
		}

		for(String dir:indexLocations){
			File indexDir = new File(dir);
	        FSDirectory directory;
			try {
				directory = FSDirectory.getDirectory(indexDir);
			} catch (IOException e) {
				throw new SearchException("find index location error.",e);
			}
			IndexSearcher indexSearcher;
			try {
				indexSearcher = new IndexSearcher(directory);
				//在"name"和"abstracts"(摘要)上搜索
				Query query = MultiFieldQueryParser.parse(queries,
						new String[]{"name","contents"} ,
						new BooleanClause.Occur[]{BooleanClause.Occur.MUST,BooleanClause.Occur.SHOULD}
						,analyzer);

			    Hits results = indexSearcher.search(query);
			    System.out.println(results.length() + " search results for query " + queryString);
			    return results;
			} catch (Exception e) {
				throw new SearchException("search error.",e);
			}

		}
		return null;
	}

	public Hits search(String queryString, String resource,
			String[] field) throws SearchException {
		Analyzer analyzer = new ChineseAnalyzer();
		BooleanClause.Occur[] occurs=new BooleanClause.Occur[field.length];
		java.util.Arrays.fill(occurs, BooleanClause.Occur.SHOULD);
		//组装搜索字符串数组
		if(resource!=null){
			ArrayExpander.expandAtHead(field, field.length+1);
			field[0]="name";
			ArrayExpander.expandAtHead(occurs, field.length);
			occurs[0]=BooleanClause.Occur.MUST;
		}


		for(String dir:indexLocations){
			File indexDir = new File(dir);
	        FSDirectory directory;
			try {
				directory = FSDirectory.getDirectory(indexDir);
			} catch (IOException e) {
				throw new SearchException("find index location error.",e);
			}
			IndexSearcher indexSearcher;
			try {
				indexSearcher = new IndexSearcher(directory);
				//在"name"和"abstracts"(摘要)上搜索
				Query query = MultiFieldQueryParser.parse(queryString,
						field,
						occurs
						,analyzer);
			    Hits results = indexSearcher.search(query);
			    System.out.println(results.length() + " search results for query " + queryString);
			    return results;
			} catch (Exception e) {
				throw new SearchException("search error.",e);
			}
		}
		return null;
	}

}
